aboutsummaryrefslogtreecommitdiff
path: root/src/routes/grocery/[id]
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-10-18 22:22:55 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-10-18 22:22:55 +0200
commit4efed80a2f8dc238b420eadf97d56c3eccaadb76 (patch)
tree7732a80745622c097a9e75dfb056603bceb10a23 /src/routes/grocery/[id]
parent0685e9ece66fbcb09c9457027cf7bb2107cc0171 (diff)
refactor: inject the database into every servicemain
Groceries, projects, references and routines become classes holding the Drizzle handle, wired once in services/index.ts alongside categories. Tests build their own instance on an in-memory database. Drops the unused projects.loadAll and projects.rename.
Diffstat (limited to 'src/routes/grocery/[id]')
-rw-r--r--src/routes/grocery/[id]/+page.server.ts34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/routes/grocery/[id]/+page.server.ts b/src/routes/grocery/[id]/+page.server.ts
index 0f386f3..dbfecff 100644
--- a/src/routes/grocery/[id]/+page.server.ts
+++ b/src/routes/grocery/[id]/+page.server.ts
@@ -2,14 +2,11 @@ import { error, fail, redirect } from '@sveltejs/kit';
import { db } from '$lib/server/db';
import { groceries, categories } from '$lib/server/db/schema';
import { eq } from 'drizzle-orm';
-import { categoriesService } from '$lib/server/services';
import {
- loadForGrocery,
- add as addRef,
- promote,
- deleteReference,
-} from '$lib/server/services/references';
-import { updateGrocery, deleteGrocery } from '$lib/server/services/groceries';
+ categoriesService,
+ groceriesService,
+ referencesService,
+} from '$lib/server/services';
import { idAction } from '$lib/server/services/grocery-actions';
import {
numericId,
@@ -39,7 +36,7 @@ export const load: PageServerLoad = async ({ params, url }) => {
.where(eq(categories.id, item.categoryId))
: [null];
- const refs = await loadForGrocery(id);
+ const refs = await referencesService.loadForGrocery(id);
const cats = await categoriesService.loadAll();
const from = url.searchParams.get('from') || '/';
@@ -67,14 +64,25 @@ export const actions: Actions = {
return fail(400, { error: 'La référence est requise' });
}
- await addRef(groceryId, reference, price, unit, qualityScore);
+ await referencesService.add(
+ groceryId,
+ reference,
+ price,
+ unit,
+ qualityScore,
+ );
return { success: true, type: 'add' as const };
},
delete: ({ request }) =>
- idAction(request, deleteReference, { type: 'delete' }),
+ idAction(request, (id) => referencesService.deleteReference(id), {
+ type: 'delete',
+ }),
- promote: ({ request }) => idAction(request, promote, { type: 'promote' }),
+ promote: ({ request }) =>
+ idAction(request, (id) => referencesService.promote(id), {
+ type: 'promote',
+ }),
update: async ({ request, params }) => {
const data = await request.formData();
@@ -88,7 +96,7 @@ export const actions: Actions = {
}
try {
- await updateGrocery(groceryId, name, categoryId);
+ await groceriesService.updateGrocery(groceryId, name, categoryId);
} catch (error) {
if (isUniqueViolation(error)) {
return fail(400, { error: 'Un article porte déjà ce nom' });
@@ -100,7 +108,7 @@ export const actions: Actions = {
deleteItem: async ({ params, request }) => {
const groceryId = numericId(params.id);
- await deleteGrocery(groceryId);
+ await groceriesService.deleteGrocery(groceryId);
const data = await request.formData();
const from = parseFormString(data.get('from')) || '/';
throw redirect(303, from);